#!/usr/bin/perl -w
##############################################################################
#
# test_Node_Obj.pl
# ================
#
# Author: Steven Liang <stliang@gmail.com>
#
# Purpose:
#   Instantiate the Node.pm class and demonstrate its various methods.  It is
#   useful to know that you can access the Expect object directly from the Node
#   object.  Any class inherits Node.pm would have the same capabilities.
#
# Steps:
#
#   1.) Instantiate a Node object and call it MyProduct
#   2.) Start a telnet session to target host
#   3.) Log the entire telnet session to a file
#   4.) Save MyProduct's Expect object pointer
#   5.) Login to the host
#   6.) Demo how to use Expect oject directly by
#       dereference expPtr.  This is useful in case
#       you need to access all Expect's functions.
#   7.) Do execute the "ls -l" command using Node's
#       short hand send expect command
#   8.) Extract session output from send command
#   9.) Release MyProduct's resource
#   10.) Release this script's resource
#
# Expected result:
#   
#   Telnet to local host and perform the ls -l command and exit cleanly.
#
##############################################################################


package main;
$VERSION = 1.01;

# If the PERLLIB environment variable is not set like
# "export PERLLIB=<lib directory>" then uncomment the
# following two lines:
# use FindBin qw($Bin);
# use lib "$Bin/./lib";
use Node;
use Setup_A;
use TestNodeRC;

my $expPtr = undef;
my $MyProduct = undef;

if ((defined $ENV{'PAT_ROOT'}) && (defined $ENV{'PAT_LOG'})) {

  # Instantiate a Node object and call it MyProduct
  $MyProduct = Node->new($Setup_A::host, $Setup_A::userName, $Setup_A::passWord, $Setup_A::port);

  # Start a telnet session to target host
  $MyProduct->openTelnet();

  # Log the entire telnet session to a file
  $MyProduct->log($ENV{'PAT_LOG'} . $TestNodeRC::logFileName);
 
  # Save MyProduct's Expect object pointer 
  $expPtr = $MyProduct->getExpPtr();

  # Login the host
  $MyProduct->login(qr/login: $/, qr/Password: $/, qr/\$/);

  # Demo how to use Expect oject directly by
  # dereference expPtr.  This is useful in case
  # you need to access all Expect's functions.
  $$expPtr->send("ls -l\n");
  $$expPtr->expect(10,"\$");
  $cmdOutPut1 = $$expPtr->before();
  $MyProduct->logMessage("\n==>Command output using expPtr:\n$cmdOutPut1\n");

  # Do execute the same command using Node's
  # short hand send expect command
  $MyProduct->send("ls -l\n");

  # Extract session output from send command
  $cmdOutPut2 = $MyProduct->getOutPut();
  $MyProduct->logMessage("\n==>Command output using MyProduct:\n $cmdOutPut2\n");

  # Release MyProduct's resource
  $MyProduct->DESTROY();
}

# Release this script's resource
undef $expPtr;
undef $MyProduct;
